<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>学生成绩管理系统</title>
  <style>
    :root {
      --primary-color: #42b983;
      --secondary-color: #35495e;
      --light-color: #f5f5f5;
      --danger-color: #ff4d4f;
      --border-color: #e8e8e8;
    }

    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }

    body {
      font-family: 'PingFang SC', 'Microsoft YaHei', sans-serif;
      line-height: 1.6;
      color: #333;
      background-color: #f9f9f9;
    }

    .container {
      max-width: 1200px;
      margin: 0 auto;
      padding: 20px;
    }

    header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;
      padding-bottom: 15px;
      border-bottom: 1px solid var(--border-color);
    }

    h1 {
      color: var(--secondary-color);
      font-size: 24px;
    }

    .btn {
      padding: 8px 15px;
      background-color: var(--primary-color);
      color: white;
      border: none;
      border-radius: 4px;
      cursor: pointer;
      font-size: 14px;
      transition: background-color 0.3s;
    }

    .btn:hover {
      background-color: #3a9170;
    }

    .btn-danger {
      background-color: var(--danger-color);
    }

    .btn-danger:hover {
      background-color: #ff2a2e;
    }

    .form-container {
      background-color: white;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
      margin-bottom: 20px;
    }

    .form-group {
      margin-bottom: 15px;
    }

    label {
      display: block;
      margin-bottom: 5px;
      font-weight: 500;
    }

    input, select {
      width: 100%;
      padding: 8px 12px;
      border: 1px solid var(--border-color);
      border-radius: 4px;
      font-size: 14px;
    }

    input.error {
      border-color: var(--danger-color);
    }

    .error-message {
      color: var(--danger-color);
      font-size: 12px;
      margin-top: 5px;
      display: none;
    }

    .students-container {
      background-color: white;
      border-radius: 8px;
      box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
      overflow: hidden;
    }

    .students-header {
      display: flex;
      padding: 15px;
      background-color: var(--secondary-color);
      color: white;
    }

    .students-header div {
      flex: 1;
      text-align: center;
    }

    .students-list {
      position: relative;
      height: 400px;
      overflow-y: auto;
    }

    .virtual-scroll {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 1px;
    }

    .student-item {
      display: flex;
      padding: 15px;
      border-bottom: 1px solid var(--border-color);
      cursor: pointer;
      transition: background-color 0.2s;
    }

    .student-item:hover {
      background-color: #f5f7fa;
    }

    .student-item div {
      flex: 1;
      text-align: center;
    }

    .student-detail {
      display: none;
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.5);
      z-index: 1000;
    }

    .detail-card {
      position: relative;
      background-color: white;
      width: 500px;
      margin: 50px auto;
      padding: 20px;
      border-radius: 8px;
      box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
    }

    .close-btn {
      position: absolute;
      top: 10px;
      right: 10px;
      font-size: 20px;
      cursor: pointer;
      color: #999;
    }

    .close-btn:hover {
      color: #333;
    }

    .detail-card h2 {
      margin-bottom: 15px;
      color: var(--secondary-color);
      text-align: center;
    }

    .detail-info {
      display: grid;
      grid-template-columns: 1fr 1fr;
      gap: 20px;
    }

    .detail-item {
      margin-bottom: 10px;
    }

    .detail-label {
      font-weight: bold;
      margin-bottom: 5px;
    }

    .pagination {
      display: flex;
      justify-content: center;
      margin-top: 20px;
    }

    .pagination button {
      padding: 5px 10px;
      margin: 0 5px;
      border: 1px solid var(--border-color);
      background: white;
      cursor: pointer;
    }

    .pagination button.active {
      background-color: var(--primary-color);
      color: white;
      border-color: var(--primary-color);
    }

    @media (max-width: 768px) {
      .students-header div {
        font-size: 14px;
        padding: 10px 5px;
      }

      .student-item div {
        font-size: 14px;
        padding: 10px 5px;
      }

      .detail-info {
        grid-template-columns: 1fr;
      }
    }
  </style>
</head>
<body>
  <div class="container">
    <header>
      <h1>学生成绩管理系统</h1>
      <button class="btn" id="addStudentBtn">添加学生</button>
    </header>

    <div class="form-container" id="studentForm" style="display: none;">
      <h2>添加学生</h2>
      <div class="form-group">
        <label for="studentName">姓名</label>
        <input type="text" id="studentName" placeholder="请输入学生姓名">
        <div class="error-message" id="nameError">姓名不能为空</div>
      </div>
      <div class="form-group">
        <label for="mathScore">数学成绩</label>
        <input type="text" id="mathScore" placeholder="0-100之间的数字">
        <div class="error-message" id="mathError">请输入0-100之间的有效数字</div>
      </div>
      <div class="form-group">
        <label for="englishScore">英语成绩</label>
        <input type="text" id="englishScore" placeholder="0-100之间的数字">
        <div class="error-message" id="englishError">请输入0-100之间的有效数字</div>
      </div>
      <div style="display: flex; justify-content: flex-end;">
        <button class="btn btn-danger" id="cancelBtn">取消</button>
        <button class="btn" id="saveStudentBtn" style="margin-left: 10px;">保存</button>
      </div>
    </div>

    <div class="students-container">
      <div class="students-header">
        <div>姓名</div>
        <div>数学</div>
        <div>英语</div>
        <div>平均分</div>
        <div>操作</div>
      </div>
      <div class="students-list" id="studentsList">
        <!-- 学生列表将通过JavaScript动态渲染 -->
      </div>
    </div>

    <div class="pagination" id="pagination">
      <!-- 分页将通过JavaScript动态渲染 -->
    </div>
  </div>

  <div class="student-detail" id="studentDetail">
    <div class="detail-card">
      <span class="close-btn" id="closeDetail">&times;</span>
      <h2 id="detailName">学生详情</h2>
      <div class="detail-info">
        <div class="detail-item">
          <div class="detail-label">数学成绩</div>
          <div id="detailMath">-</div>
        </div>
        <div class="detail-item">
          <div class="detail-label">英语成绩</div>
          <div id="detailEnglish">-</div>
        </div>
        <div class="detail-item">
          <div class="detail-label">平均分</div>
          <div id="detailAverage">-</div>
        </div>
      </div>
    </div>
  </div>

  <script>
    // 学生数据
    const students = [
      { id: 1, name: "张三", math: 90, english: 85 },
      { id: 2, name: "李四", math: 78, english: 92 },
      { id: 3, name: "王五", math: 85, english: 88 },
      { id: 4, name: "赵六", math: 92, english: 95 },
      { id: 5, name: "钱七", math: 88, english: 89 },
      { id: 6, name: "孙八", math: 76, english: 82 },
      { id: 7, name: "周九", math: 80, english: 90 },
      { id: 8, name: "吴十", math: 84, english: 87 },
      { id: 9, name: "郑十一", math: 95, english: 83 },
      { id: 10, name: "王十二", math: 70, english: 90 },
      { id: 11, name: "冯十三", math: 85, english: 85 },
      { id: 12, name: "陈十四", math: 91, english: 93 },
      { id: 13, name: "楚十五", math: 77, english: 88 },
      { id: 14, name: "韩十六", math: 82, english: 91 },
      { id: 15, name: "杨十七", math: 89, english: 84 },
      { id: 16, name: "朱十八", math: 79, english: 87 },
      { id: 17, name: "秦十九", math: 81, english: 86 },
      { id: 18, name: "尤二十", math: 93, english: 90 },
      { id: 19, name: "许二十一", math: 75, english: 82 },
      { id: 20, name: "何二十二", math: 87, english: 94 }
    ];

    // 当前排序字段
    let currentSort = null;
    // 下一个学生ID
    let nextId = students.length + 1;
    // 每页显示的学生数量
    const itemsPerPage = 10;
    // 当前页码
    let currentPage = 1;
    // 所有DOM元素
    const studentForm = document.getElementById('studentForm');
    const studentsList = document.getElementById('studentsList');
    const addStudentBtn = document.getElementById('addStudentBtn');
    const cancelBtn = document.getElementById('cancelBtn');
    const saveStudentBtn = document.getElementById('saveStudentBtn');
    const studentDetail = document.getElementById('studentDetail');
    const closeDetail = document.getElementById('closeDetail');
    const pagination = document.getElementById('pagination');

    // 初始化页面
    document.addEventListener('DOMContentLoaded', () => {
      renderStudents();
      setupEventListeners();
    });

    // 设置事件监听器
    function setupEventListeners() {
      // 添加学生按钮点击事件
      addStudentBtn.addEventListener('click', () => {
        studentForm.style.display = 'block';
      });

      // 取消按钮点击事件
      cancelBtn.addEventListener('click', () => {
        studentForm.style.display = 'none';
        resetForm();
      });

      // 保存学生按钮点击事件
      saveStudentBtn.addEventListener('click', saveStudent);

      // 关闭详情弹窗
      closeDetail.addEventListener('click', () => {
        studentDetail.style.display = 'none';
      });

      // 添加表单验证
      document.getElementById('studentName').addEventListener('input', () => {
        validateInput('studentName', 'nameError');
      });

      document.getElementById('mathScore').addEventListener('input', () => {
        validateInput('mathScore', 'mathError');
      });

      document.getElementById('englishScore').addEventListener('input', () => {
        validateInput('englishScore', 'englishError');
      });
    }

    // 渲染学生列表
    function renderStudents() {
      // 清空列表
      studentsList.innerHTML = '';

      // 获取当前页面的学生
      const startIndex = (currentPage - 1) * itemsPerPage;
      const endIndex = Math.min(startIndex + itemsPerPage, students.length);
      const currentStudents = students.slice(startIndex, endIndex);

      // 创建虚拟滚动容器
      const virtualScroll = document.createElement('div');
      virtualScroll.className = 'virtual-scroll';

      // 渲染学生列表
      currentStudents.forEach((student, index) => {
        const studentItem = document.createElement('div');
        studentItem.className = 'student-item';

        // 计算平均分
        const average = ((student.math + student.english) / 2).toFixed(1);

        studentItem.innerHTML = `
          <div>${student.name}</div>
          <div>${student.math}</div>
          <div>${student.english}</div>
          <div>${average}</div>
          <div>
            <button class="btn btn-danger delete-btn" data-id="${student.id}">删除</button>
          </div>
        `;

        // 点击学生姓名显示详情
        studentItem.children[0].addEventListener('click', () => {
          showStudentDetail(student);
        });

        // 点击删除按钮
        const deleteBtn = studentItem.querySelector('.delete-btn');
        deleteBtn.addEventListener('click', (e) => {
          e.stopPropagation();
          deleteStudent(student.id);
        });

        // 添加到虚拟滚动容器
        virtualScroll.appendChild(studentItem);
      });

      // 设置虚拟滚动容器高度
      const itemHeight = 60; // 每个学生项的高度
      virtualScroll.style.height = `${itemHeight * students.length}px`;
      studentsList.appendChild(virtualScroll);

      // 渲染分页
      renderPagination();
    }

    // 渲染分页
    function renderPagination() {
      const totalPages = Math.ceil(students.length / itemsPerPage);
      if (totalPages <= 1) {
        pagination.style.display = 'none';
        return;
      }

      pagination.style.display = 'flex';
      pagination.innerHTML = '';

      // 上一页按钮
      const prevBtn = document.createElement('button');
      prevBtn.textContent = '上一页';
      prevBtn.disabled = currentPage === 1;
      prevBtn.addEventListener('click', () => {
        if (currentPage > 1) {
          currentPage--;
          renderStudents();
        }
      });
      pagination.appendChild(prevBtn);

      // 页码按钮
      for (let i = 1; i <= totalPages; i++) {
        const pageBtn = document.createElement('button');
        pageBtn.textContent = i;
        pageBtn.classList.add('page-btn');
        if (i === currentPage) {
          pageBtn.classList.add('active');
        }
        pageBtn.addEventListener('click', () => {
          currentPage = i;
          renderStudents();
        });
        pagination.appendChild(pageBtn);
      }

      // 下一页按钮
      const nextBtn = document.createElement('button');
      nextBtn.textContent = '下一页';
      nextBtn.disabled = currentPage === totalPages;
      nextBtn.addEventListener('click', () => {
        if (currentPage < totalPages) {
          currentPage++;
          renderStudents();
        }
      });
      pagination.appendChild(nextBtn);
    }

    // 显示学生详情
    function showStudentDetail(student) {
      document.getElementById('detailName').textContent = student.name;
      document.getElementById('detailMath').textContent = student.math;
      document.getElementById('detailEnglish').textContent = student.english;
      document.getElementById('detailAverage').textContent =
        ((student.math + student.english) / 2).toFixed(1);

      studentDetail.style.display = 'block';
    }

    // 删除学生
    function deleteStudent(id) {
      if (confirm('确定要删除这个学生吗？')) {
        students = students.filter(student => student.id !== id);
        renderStudents();
      }
    }

    // 保存学生
    function saveStudent() {
      // 表单验证
      const name = document.getElementById('studentName').value.trim();
      const math = document.getElementById('mathScore').value.trim();
      const english = document.getElementById('englishScore').value.trim();

      // 验证姓名
      if (!name) {
        showError('nameError');
        return;
      }

      // 验证数学成绩
      if (!validateScore(math)) {
        showError('mathError');
        return;
      }

      // 验证英语成绩
      if (!validateScore(english)) {
        showError('englishError');
        return;
      }

      // 创建新学生对象
      const newStudent = {
        id: nextId++,
        name: name,
        math: parseInt(math),
        english: parseInt(english)
      };

      // 添加到学生列表
      students.push(newStudent);
      studentForm.style.display = 'none';
      resetForm();
      renderStudents();
    }

    // 重置表单
    function resetForm() {
      document.getElementById('studentName').value = '';
      document.getElementById('mathScore').value = '';
      document.getElementById('englishScore').value = '';
      hideAllErrors();
    }

    // 验证分数
    function validateScore(score) {
      const scoreValue = parseInt(score);
      if (isNaN(scoreValue) || scoreValue < 0 || scoreValue > 100) {
        return false;
      }
      return true;
    }

    // 验证输入
    function validateInput(inputId, errorId) {
      const value = document.getElementById(inputId).value.trim();
      if (inputId === 'studentName') {
        if (!value) {
          showError(errorId);
        } else {
          hideError(errorId);
        }
      } else {
        if (!validateScore(value)) {
          showError(errorId);
        } else {
          hideError(errorId);
        }
      }
    }

    // 显示错误信息
    function showError(errorId) {
      document.getElementById(errorId).style.display = 'block';
      document.getElementById(errorId.replace('Error', '')).classList.add('error');
    }

    // 隐藏错误信息
    function hideError(errorId) {
      document.getElementById(errorId).style.display = 'none';
      document.getElementById(errorId.replace('Error', '')).classList.remove('error');
    }

    // 隐藏所有错误信息
    function hideAllErrors() {
      hideError('nameError');
      hideError('mathError');
      hideError('englishError');
    }
  </script>
</body>
</html>